package sys

import (
	"github.com/gin-gonic/gin"
	"github.com/gookit/validate"
	"strconv"
	"strings"
	"xkginweb/commons/response"
	"xkginweb/global"
	context2 "xkginweb/model/context"
	req "xkginweb/model/entity/comms/request"
	resp "xkginweb/model/entity/comms/response"
	"xkginweb/model/entity/sys"
	"xkginweb/utils"
)

type SysUsersApi struct{}

// 修改密码
func (api *SysUsersApi) UpdatePwd(c *gin.Context) {
	// 1: 第一件事情就准备数据的载体
	var sysUserPwdContext context2.SysUserPwdContext
	err := c.ShouldBindJSON(&sysUserPwdContext)
	if err != nil {
		// 如果参数注入失败或者出错就返回接口调用这。出错了.
		response.FailWithMessage(err.Error(), c)
		return
	}

	validation := validate.Struct(sysUserPwdContext)
	if !validation.Validate() {
		response.FailWithValidatorData(validation, c)
		return
	}

	if sysUserPwdContext.Password != sysUserPwdContext.ConfirmPassword {
		response.FailWithMessage("两次输入的密码不一致!", c)
		return
	}

	user, _ := sysUserService.GetSysUserByID(c.GetUint("userId"))
	user.Slat = utils.GetUUID()
	newPassword := utils.Md5Slat(sysUserPwdContext.Password, user.Slat)

	// 开始更新密码
	sysUser := sys.SysUser{}
	sysUser.ID = user.ID
	sysUser.Password = newPassword
	sysUser.Slat = user.Slat
	// 开始执行密码更新
	sysUserService.UpdateSysUser(&sysUser)
	response.OkSuccess(c)
}

// 保存
func (api *SysUsersApi) SaveData(c *gin.Context) {
	// 1: 第一件事情就准备数据的载体
	var sysUserContext context2.SysUserContext
	err := c.ShouldBindJSON(&sysUserContext)
	if err != nil {
		// 如果参数注入失败或者出错就返回接口调用这。出错了.
		response.FailWithMessage(err.Error(), c)
		return
	}

	validation := validate.Struct(sysUserContext)
	if !validation.Validate() {
		response.FailWithValidatorData(validation, c)
		return
	}

	// 校验----框架
	sysUser := sys.SysUser{}
	// 对象复制
	utils.CopyProperties(&sysUser, sysUserContext)
	// 设置uuid
	sysUser.UUID = utils.GetUUID()
	// 设置密码盐
	sysUser.Slat = utils.GetUUID()
	// 密码加密
	sysUser.Password = utils.Md5Slat(sysUser.Password, sysUser.Slat)
	// 创建实例，保存帖子
	err = sysUserService.SaveSysUser(&sysUser)
	// 如果保存失败。就返回创建失败的提升
	if err != nil {
		response.FailWithMessage("创建失败", c)
		return
	}
	// 如果保存成功，就返回创建创建成功
	response.Ok("创建成功", c)
}

// 修改
func (api *SysUsersApi) UpdateById(c *gin.Context) {
	// 1: 第一件事情就准备数据的载体
	var sysUser sys.SysUser
	err := c.ShouldBindJSON(&sysUser)
	if err != nil {
		// 如果参数注入失败或者出错就返回接口调用这。出错了.
		response.FailWithMessage(err.Error(), c)
		return
	}

	err = sysUserService.UpdateSysUser(&sysUser)
	// 如果保存失败。就返回创建失败的提升
	if err != nil {
		response.FailWithMessage("更新失败", c)
		return
	}
	// 如果保存成功，就返回创建创建成功
	response.Ok("更新成功", c)
}

// 根据id删除
func (api *SysUsersApi) DeleteById(c *gin.Context) {
	// 绑定参数用来获取/:id这个方式
	id := c.Param("id")
	// 开始执行
	parseUint, _ := strconv.ParseUint(id, 10, 64)
	err := sysUserService.DelSysUserById(uint(parseUint))
	if err != nil {
		response.FailWithMessage("删除失败", c)
		return
	}
	response.Ok("ok", c)
}

// 批量删除
func (api *SysUsersApi) DeleteByIds(c *gin.Context) {
	// 绑定参数用来获取/:id这个方式
	ids := c.Query("ids")
	// 开始执行
	idstrings := strings.Split(ids, ",")
	var sysUsers []sys.SysUser
	for _, id := range idstrings {
		parseUint, _ := strconv.ParseUint(id, 10, 64)
		sysUser := sys.SysUser{}
		sysUser.ID = uint(parseUint)
		sysUsers = append(sysUsers, sysUser)
	}
	err := sysUserService.DeleteSysUsersByIds(sysUsers)
	if err != nil {
		response.FailWithMessage("删除失败", c)
		return
	}
	response.Ok("ok", c)
}

// 根据id查询信息
func (api *SysUsersApi) GetById(c *gin.Context) {
	// 根据id查询方法
	id := c.Param("id")
	// 根据id查询方法
	parseUint, _ := strconv.ParseUint(id, 10, 64)
	sysUser, err := sysUserService.GetSysUserByID(uint(parseUint))
	if err != nil {
		global.SugarLog.Errorf("查询用户: %s 失败", id)
		response.FailWithMessage("查询用户失败", c)
		return
	}

	response.Ok(sysUser, c)
}

// 分页查询信息
func (api *SysUsersApi) LoadSysUserPage(c *gin.Context) {
	// 创建一个分页对象
	var pageInfo req.PageInfo
	// 把前端json的参数传入给PageInfo
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	sysUserPage, total, err := sysUserService.LoadSysUserPage(pageInfo)
	if err != nil {
		response.FailWithMessage("获取失败"+err.Error(), c)
		return
	}

	response.Ok(resp.PageResult{
		List:     sysUserPage,
		Total:    total,
		Page:     pageInfo.Page,
		PageSize: pageInfo.PageSize,
	}, c)
}
